package com.hgw.gulimall.gateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;

/**
 * Data time:2022/3/14 21:17
 * StudentID:2019112118
 * Author:hgw
 * Description: 配置跨域，该类用来做过滤，允许所有的请求跨域。
 */
@Configuration
public class GulimallCorsConfiguration {

    @Bean
    public CorsWebFilter corsWebFilter() {
        // 基于url跨域，选择reactive包下的
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();

        // 配置跨域信息
        CorsConfiguration configuration = new CorsConfiguration();
        // 允许跨域的头 *：表示所有
        configuration.addAllowedHeader("*");
        // 允许跨域的请求方式
        configuration.addAllowedMethod("*");
        // 允许跨域的请求来源
        configuration.addAllowedOrigin("*");
        // 是否允许携带cookie跨域
        configuration.setAllowCredentials(true);

        // `/**` :任意url都要进行跨域配置
        source.registerCorsConfiguration("/**",configuration);
        return new CorsWebFilter(source);
    }
}
